package com.example.cybz.common.result;

import lombok.Getter;

import java.util.Objects;

/**
 * 统一返回结果状态信息枚举类
 * 状态码规范：
 * 0000     - 全局成功
 * 0001-0999 - 全局错误
 * 1000-1999 - 认证授权相关
 * 2000-2999 - 参数校验相关
 * 3000-3999 - 业务处理相关
 * 4000-4999 - 数据操作相关
 * 5000-5999 - 第三方服务相关
 * 9000-9999 - 系统级错误
 */
@Getter
public enum ResultCodeEnum {

    /* ========== 全局状态（0000-0999） ========== */
    SUCCESS(0, "操作成功"),
    FAIL(1, "操作失败"),
    UNKNOWN_ERROR(2, "未知错误"),
    SERVICE_UNAVAILABLE(3, "服务不可用"),
    SERVICE_TIMEOUT(4, "服务超时"),
    REPEAT_SUBMIT(5, "重复提交"),
    FREQUENCY_LIMIT(6, "请求过于频繁"),
    RESOURCE_NOT_FOUND(7, "资源不存在"),
    METHOD_NOT_ALLOWED(8, "请求方法不允许"),
    UNSUPPORTED_MEDIA_TYPE(9, "不支持的媒体类型"),

    /* ========== 认证授权（1000-1999） ========== */
    UNAUTHORIZED(1000, "未认证"),
    FORBIDDEN(1001, "无访问权限"),
    LOGIN_REQUIRED(1002, "请先登录"),
    INVALID_TOKEN(1003, "无效令牌"),
    TOKEN_EXPIRED(1004, "令牌已过期"),
    CREDENTIALS_EXPIRED(1005, "凭证已过期"),
    ACCOUNT_DISABLED(1006, "账号已禁用"),
    ACCOUNT_LOCKED(1007, "账号已锁定"),
    ACCOUNT_EXPIRED(1008, "账号已过期"),
    INVALID_CREDENTIALS(1009, "用户名或密码错误"),
    SESSION_EXPIRED(1010, "会话已过期"),
    ACCESS_DENIED(1011, "访问被拒绝"),
    INVALID_VERIFICATION_CODE(1012, "验证码错误"),
    VERIFICATION_CODE_EXPIRED(1013, "验证码已过期"),
    THIRD_PARTY_AUTH_FAILED(1014, "第三方认证失败"),

    /* ========== 参数校验（2000-2999） ========== */
    PARAM_ERROR(2000, "参数错误"),
    PARAM_MISSING(2001, "参数缺失"),
    PARAM_TYPE_MISMATCH(2002, "参数类型不匹配"),
    PARAM_BIND_ERROR(2003, "参数绑定错误"),
    PARAM_VALIDATION_FAILED(2004, "参数校验失败"),
    PARAM_FORMAT_INVALID(2005, "参数格式无效"),
    ILLEGAL_REQUEST(2006, "非法请求"),
    REQUEST_BODY_MISSING(2007, "请求体缺失"),
    REQUEST_BODY_INVALID(2008, "请求体无效"),

    /* ========== 业务处理（3000-3999） ========== */
    BUSINESS_ERROR(3000, "业务处理异常"),
    OPERATION_NOT_ALLOWED(3001, "不允许的操作"),
    STATE_TRANSITION_ERROR(3002, "状态转换错误"),
    DUPLICATE_OPERATION(3003, "重复操作"),
    INSUFFICIENT_BALANCE(3004, "余额不足"),
    LIMIT_EXCEEDED(3005, "超出限制"),
    CONFLICT_OPERATION(3006, "操作冲突"),
    RESOURCE_OCCUPIED(3007, "资源已被占用"),
    VERSION_MISMATCH(3008, "版本不匹配"),
    QUOTA_EXCEEDED(3009, "超出配额"),

    /* ========== 订单业务（3100-3199） ========== */
    ORDER_ERROR(3100, "订单异常"),
    ORDER_PRICE_CHANGED(3101, "订单价格变化"),
    ORDER_STOCK_LOCK_FAILED(3102, "订单库存锁定失败"),
    ORDER_CREATE_FAILED(3103, "创建订单失败"),
    ORDER_CANCELED(3104, "订单已取消"),
    ORDER_PAID(3105, "订单已支付"),
    ORDER_COMPLETED(3106, "订单已完成"),
    ORDER_NOT_EXIST(3107, "订单不存在"),
    ORDER_STATUS_ERROR(3108, "订单状态错误"),

    /* ========== 库存业务（3200-3299） ========== */
    INSUFFICIENT_STOCK(3200, "库存不足"),
    STOCK_LOCK_FAILED(3201, "库存锁定失败"),
    STOCK_RELEASE_FAILED(3202, "库存释放失败"),
    STOCK_DEDUCT_FAILED(3203, "库存扣减失败"),
    STOCK_ROLLBACK_FAILED(3204, "库存回滚失败"),

    /* ========== 优惠券业务（3300-3399） ========== */
    COUPON_ERROR(3300, "优惠券异常"),
    COUPON_ALREADY_CLAIMED(3301, "优惠券已领取"),
    COUPON_EXHAUSTED(3302, "优惠券已发放完毕"),
    COUPON_EXPIRED(3303, "优惠券已过期"),
    COUPON_NOT_APPLICABLE(3304, "优惠券不适用"),
    COUPON_LIMIT_REACHED(3305, "优惠券领取上限"),
    COUPON_NOT_FOUND(3306, "优惠券不存在"),

    /* ========== 商品业务（3400-3499） ========== */
    PRODUCT_ERROR(3400, "商品异常"),
    PRODUCT_NOT_FOUND(3401, "商品不存在"),
    PRODUCT_OFF_SHELF(3402, "商品已下架"),
    PRODUCT_SKU_LIMIT(3403, "超过限购数量"),
    PRODUCT_INVENTORY_ERROR(3404, "商品库存异常"),

    /* ========== 数据操作（4000-4999） ========== */
    DATA_ERROR(4000, "数据异常"),
    DATA_NOT_FOUND(4001, "数据不存在"),
    DATA_ALREADY_EXISTS(4002, "数据已存在"),
    DATA_INTEGRITY_VIOLATION(4003, "数据完整性违规"),
    DATA_ACCESS_ERROR(4004, "数据访问错误"),
    DATA_VALIDATION_FAILED(4005, "数据验证失败"),
    DATA_CONVERSION_ERROR(4006, "数据转换错误"),
    DATA_VERSION_CONFLICT(4007, "数据版本冲突"),
    DATA_DUPLICATE_KEY(4008, "数据唯一键冲突"),

    /* ========== 第三方服务（5000-5999） ========== */
    THIRD_PARTY_ERROR(5000, "第三方服务异常"),
    THIRD_PARTY_TIMEOUT(5001, "第三方服务超时"),
    THIRD_PARTY_UNAVAILABLE(5002, "第三方服务不可用"),
    THIRD_PARTY_AUTH_ERROR(5003, "第三方认证错误"),

    /* ========== 支付服务（5100-5199） ========== */
    PAYMENT_ERROR(5100, "支付异常"),
    PAYMENT_FAILED(5101, "支付失败"),
    PAYMENT_TIMEOUT(5102, "支付超时"),
    PAYMENT_REFUND_FAILED(5103, "退款失败"),
    PAYMENT_AMOUNT_MISMATCH(5104, "支付金额不匹配"),
    PAYMENT_ORDER_NOT_EXIST(5105, "支付订单不存在"),

    /* ========== 短信服务（5200-5299） ========== */
    SMS_SEND_FAILED(5200, "短信发送失败"),
    SMS_LIMIT_EXCEEDED(5201, "短信发送频率超限"),
    SMS_TEMPLATE_INVALID(5202, "短信模板无效"),

    /* ========== OAuth认证（5300-5399） ========== */
    OAUTH_ERROR(5300, "OAuth认证异常"),
    URL_ENCODE_ERROR(5301, "URL编码失败"),
    ILLEGAL_CALLBACK_REQUEST(5302, "非法回调请求"),
    ACCESS_TOKEN_FETCH_FAILED(5303, "获取accessToken失败"),
    USER_INFO_FETCH_FAILED(5304, "获取用户信息失败"),
    REFRESH_TOKEN_EXPIRED(5305, "refreshToken已过期"),

    /* ========== 区域服务（5400-5499） ========== */
    REGION_OPEN(5400, "该区域已开通"),
    REGION_NOT_OPEN(5401, "该区域未开通"),
    REGION_NOT_SUPPORTED(5402, "该区域不支持"),

    /* ========== 系统级错误（9000-9999） ========== */
    SYSTEM_ERROR(9000, "系统错误"),
    DATABASE_ERROR(9001, "数据库错误"),
    NETWORK_ERROR(9002, "网络错误"),
    CONFIGURATION_ERROR(9003, "配置错误"),
    FILE_OPERATION_ERROR(9004, "文件操作错误"),
    ENCRYPTION_ERROR(9005, "加密错误"),
    DECRYPTION_ERROR(9006, "解密错误"),
    IO_ERROR(9007, "IO异常");

    private final Integer code;
    private final String message;

    ResultCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据状态码查找对应的枚举
     *
     * @param code 状态码
     * @return 对应的枚举，找不到时返回UNKNOWN_ERROR
     */
    public static ResultCodeEnum getByCode(Integer code) {
        if (code == null) {
            return UNKNOWN_ERROR;
        }

        for (ResultCodeEnum value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }

        return UNKNOWN_ERROR;
    }

    /**
     * 判断是否为成功状态码
     *
     * @param code 状态码
     * @return true 表示成功，false 表示失败
     */
    public static boolean isSuccess(Integer code) {
        return Objects.nonNull(code) && code.equals(SUCCESS.code);
    }
}
